package com.sdry.web.controller.zc.quality;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.llm.User;
import com.sdry.model.zc.ZcDisposeRejectsDetailsEntity;
import com.sdry.model.zc.ZcDisposeRejectsEntity;
import com.sdry.model.zc.ZcQueryCriteria;
import com.sdry.model.zc.ZcReworkEntity;
import com.sdry.service.zc.ZcReworkService;
import com.sdry.utils.ResponseUtil;

/**
 * 
 * @ClassName:    ZcReworkController   
 * @Description:  膜组件返工
 * @Author:       zc   
 * @CreateDate:   2018年10月16日 下午3:32:18   
 * @Version:      v1.0
 */
@Controller
@RequestMapping("rework")
public class ZcReworkController {

	@Resource
	private ZcReworkService zcReworkService;
	/**
	 * 进入膜组件返工页面
	 * @return
	 */
	@RequestMapping("enterReworkPage")
	public String enterReworkPage(){
		return "/zc/quality/rework";
	}
	/**
	 * 查询返工列表
	 * @param request
	 * @param response
	 */
	@RequestMapping("selectReworkList")
	public void selectReworkList(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria){
		if(zcQueryCriteria.getKeyword()==null){
			zcQueryCriteria.setKeyword("");
		}
		List<ZcReworkEntity> reworkList = zcReworkService.selectReworkList(zcQueryCriteria);
		int count = zcReworkService.countReworkList(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(reworkList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 查询待返工处置单列表
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 */
	@RequestMapping("selectStayReworkList")
	public void selectStayReworkList(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria){
		List<ZcDisposeRejectsDetailsEntity> stayReworkList = zcReworkService.selectStayReworkList(zcQueryCriteria);
		int count = zcReworkService.countStayReworkList(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(stayReworkList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过id查询处置单详情
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 */
	@RequestMapping("selectDisposeRejectsDetailsById")
	public void selectDisposeRejectsDetailsById(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria){
		ZcDisposeRejectsDetailsEntity zcDisposeRejectsDetailsEntity = zcReworkService.selectRework(zcQueryCriteria);
		JSONObject json= JSONObject.fromObject(zcDisposeRejectsDetailsEntity);  
		try {
			if(zcDisposeRejectsDetailsEntity!=null){
				ResponseUtil.write(response, json);
			}else{
				ResponseUtil.write(response, 0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 保存返工单
	 * @param request
	 * @param response
	 * @param zcReworkEntity
	 */
	@RequestMapping("insertRework")
	public void insertRework(HttpServletRequest request,HttpServletResponse response,ZcReworkEntity zcReworkEntity,HttpSession httpSession){
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		String fgNumber = "FG"+System.currentTimeMillis();
		zcReworkEntity.setFgNumber(fgNumber);
		zcReworkEntity.setFgStatus("0");
		zcReworkEntity.setFgDisponer(userName);
		if(zcReworkEntity.getFgReworkDate()==""){
			zcReworkEntity.setFgReworkDate(null);
		}
		int affact = zcReworkService.insertRework(zcReworkEntity);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 批量删除
	 * @param request
	 * @param response
	 * @param idArr
	 */
	@RequestMapping("delReworkArr")
	public void delReworkArr(HttpServletRequest request,HttpServletResponse response,@RequestParam("idArr[]") String[] idArr){
		int affact = 0;
		for (String fgid : idArr) {
			long id = Long.parseLong(fgid);
			affact += zcReworkService.delReworkArr(id);
		}
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
